﻿
Imports DTO
Imports BUS

Public Class frmTHEMKHACHHANG

    Sub rong()

        txt_TenKH.Text = ""
        txt_diachi.Text = ""
        txt_sdt.Text = ""

    End Sub

    Dim khbus As New KhachHangBUS
    Dim KHdto As New KhachHangDTO

    Private Sub btn_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_them.Click
        If txt_diachi.Text <> "" And txt_sdt.Text <> "" And txt_TenKH.Text <> "" Then
           
            KHdto.TenKH = txt_TenKH.Text
            KHdto.DiaChi = txt_diachi.Text
            KHdto.SDT = txt_sdt.Text
            KHdto.MaHD = cbo_mahd.SelectedValue
            khbus.ThemKH(KHdto)
            LoadDSKH()
            MessageBox.Show("Da Them Thanh Cong")
        Else
            MessageBox.Show("Xin Nhap Day Du Thong Tin!", "Thông Báo")
        End If

    End Sub


    Public Sub LoadDSKH()
        dgv_ThongTinKH.DataSource = KhachHangBUS.LayTATCa()
    End Sub


    Private Sub btn_Nhaplai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Nhaplai.Click
        rong()
    End Sub



    Private Sub frmTHEMKHACHHANG_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
        LoadDSKH()
        loadmahd()
    End Sub

    Private Sub loadmahd()
        cbo_mahd.DataSource = HoaDonBUS.layKHtheoid()
        cbo_mahd.DisplayMember = "MaHD"
        cbo_mahd.ValueMember = "MaHD"
    End Sub
    Private Sub btn_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_thoat.Click
        Me.Close()
    End Sub

    Private Sub btn_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_xoa.Click
        Dim dial = MessageBox.Show("Bạn có thật sự muốn xóa khách hàng này không? ",
                                           "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dial = DialogResult.Yes Then
            Dim nv As New KhachHangDTO()
            'nv = dgv_ThongTinKH.CurrentRow.DataBoundItem
            nv.MaKH = dgv_ThongTinKH.Rows(temp).Cells(0).Value

            KhachHangBUS.XOA(nv.MaKH)
            LoadDSKH()
            MessageBox.Show("Da Xoa Thanh Cong!")
        End If
        
    End Sub

    Private Sub btn_sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sua.Click

        Dim dial = MessageBox.Show("Bạn Có Thực Sự Muốn Cập Nhập Lại Thông Tin Khách Hàng Này không?",
                                   "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dial = DialogResult.Yes Then
            capnhap()
        End If


    End Sub
    Private Sub capnhap()
        KHdto.TenKH = txt_TenKH.Text
        KHdto.DiaChi = txt_diachi.Text
        KHdto.SDT = txt_sdt.Text
        KHdto.MaHD = cbo_mahd.SelectedValue
        KHdto.MaKH = dgv_ThongTinKH.Rows(temp).Cells(0).Value
        khbus.capnhap(KHdto)
        LoadDSKH()
        MessageBox.Show("Da Cập Nhập Thanh Cong")
    End Sub
    Dim temp As Integer
    Private Sub dgv_ThongTinKH_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_ThongTinKH.RowEnter
        temp = e.RowIndex
    End Sub

    Private Sub btn_nhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nhap.Click
        NhapFileKH()
    End Sub
    Public Sub NhapFileKH()
        Dim dialog As New OpenFileDialog()
        dialog.InitialDirectory = "D:\"
        dialog.Title = "Open Excel Files"
        dialog.CheckPathExists = True
        dialog.DefaultExt = "xls"
        dialog.Filter = "Excel files (*.xls 1997 - 2003)|*.xls|Excel files (*.xls 2007-2013)|*.xlsx|All files (*.*)|*.*"
        dialog.FilterIndex = 2
        dialog.RestoreDirectory = True
        If (dialog.ShowDialog() = DialogResult.OK) Then
            ExpostToExell.ImportKH(dialog.FileName)

        End If
    End Sub
    Private Sub btn_xuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_xuat.Click
        XuatFileKH()
    End Sub
    Public Sub XuatFileKH()
        Dim dialog As New SaveFileDialog
        dialog.InitialDirectory = "D:\"
        dialog.Title = "Save Excel Files"
        dialog.CheckPathExists = True
        dialog.DefaultExt = "xls"
        dialog.Filter = "Excel files (*.xls 1997 - 2003)|*.xls|Excel files (*.xls 2007-2013)|*.xlsx|All files (*.*)|*.*"
        dialog.FilterIndex = 2
        dialog.RestoreDirectory = True
        If (dialog.ShowDialog() = DialogResult.OK) Then
            ExpostToExell.ExportKH(dialog.FileName)
            MessageBox.Show("Xuất dữ liệu ra file excel thành công", "SQL Authetication")

        End If
    End Sub
End Class